package com.baitiaojun.common.utils.date;

import org.apache.commons.lang3.time.DateUtils;

import java.lang.management.ManagementFactory;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateTimeUtils extends DateUtils {

    public static String YYYY = "yyyy";

    public static String YYYY_MM = "yyyy-MM";

    public static String YYYY_MM_DD = "yyyy-MM-dd";

    public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

    private static String[] parsePatterns = {
            "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
            "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
            "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};

    public static String parseStr(final String format, final Date date) {
        return new SimpleDateFormat(format).format(date);
    }

    /**
     * 获取服务器启动时间
     */
    public static Date getServerStartDate() {
        long time = ManagementFactory.getRuntimeMXBean().getStartTime();
        return new Date(time);
    }

    /**
     * 某个时间格式的object对象转换成对应时间格式的date对象
     * @param str
     * @return
     */
    public static Date parseDate(Object str) {
        if (str == null) {
            return null;
        }
        try {
            return parseDate(str.toString(), parsePatterns);
        } catch (ParseException e) {
            return null;
        }
    }

    public static Date parseDate(String str, String format) {
        try {
            return new SimpleDateFormat(format).parse(str);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    public static String getYear() {
        return format(getDateNow(), YYYY);
    }

    public static String getYearMonth() {
        return format(getDateNow(), YYYY_MM);
    }

    public static String getDate() {
        return format(getDateNow(), YYYY_MM_DD);
    }

    public static String getTime() {
        return format(getDateNow(), YYYYMMDDHHMMSS);
    }

    public static String getDateTime() {
        return format(getDateNow(), YYYY_MM_DD_HH_MM_SS);
    }

    public static String getDateTime(Date date) {
        return format(date, YYYY_MM_DD_HH_MM_SS);
    }

    /**
     * 日期路径 即年/月/日 如2018/08/08
     */
    public static String getSegmentDatePath() {
        return format(getDateNow(), "yyyy/MM/dd");
    }

    /**
     * 日期路径 即年月日 如20180808
     */
    public static String getDatePath() {
        return format(getDateNow(), "yyyyMMdd");
    }

    public static String format(Date date, String format) {
        return new SimpleDateFormat(format).format(date);
    }

    public static Date getDateNow() {
        return new Date();
    }

    /**
     * 计算时间差
     * @param date1
     * @param date2
     * @return
     */
    public static String dateDiff(Date date1, Date date2) {
        String day = dayDiff(date1, date2);
        String hour = hourDiff(date1, date2);
        String munit = munitDiff(date1, date2);
        return "时间相差" + day + "天" + hour + "小时" + munit + "分";
    }

    public static String dayDiff(Date date1, Date date2) {
        return timeDiff(date1, date2, 1000 * 60 * 60 * 24, "d");
    }

    public static String hourDiff(Date date1, Date date2) {
        return timeDiff(date1, date2, 1000 * 60 * 60, "h");
    }

    public static String munitDiff(Date date1, Date date2) {
        return timeDiff(date1, date2, 1000 * 60, "m");
    }

    public static String timeDiff(Date date1, Date date2, long time, String unit) {
        long diff = date1.getTime() - date2.getTime();
        //取出小时
        if ("h".equals(unit)) {
            diff = diff % (1000 * 60 * 60 * 24);
        }
        //取出分钟
        if ("m".equals(unit)) {
            diff = diff % (1000 * 60 * 60 * 24) % (1000 * 60 * 60);
        }
        long day = diff / time;
        return String.valueOf(day);
    }
}
